package com.example.utils;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;


import com.bumptech.glide.Glide;
import com.google.android.material.snackbar.Snackbar;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.indicator.RectangleIndicator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity {

    public String title;
    Banner banner;
    WebView webView;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        banner = findViewById(R.id.banner);
        webView = findViewById(R.id.webView);
        button = findViewById(R.id.button);
        initBanner();
        initWebView();
        initView();
        Handler handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
            }
        };
    }

    public void initBanner(){
        OkhttpMethod.getCall("http://124.93.196.45:10002/userinfo/rotation/list?pageNum=1&pageSize=10&type=45").enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ArrayList<PhotoBean.RowsBean> tempList = new ArrayList<>();
                PhotoBean bean = new Gson().fromJson(response.body().string(),PhotoBean.class);
                List<PhotoBean.RowsBean> list = bean.getRows();
                if (list!=null){
                    tempList.addAll(list);
                }
                banner.post(new Runnable() {
                    @Override
                    public void run() {
                        banner.setAdapter(new BannerImageAdapter<PhotoBean.RowsBean>(tempList) {

                            @Override
                            public void onBindView(BannerImageHolder bannerImageHolder, PhotoBean.RowsBean rowsBean, int i, int i1) {
                               Glide.with(bannerImageHolder.itemView).load("http://124.93.196.45:10002"+tempList.get(i).getImgUrl()).into(bannerImageHolder.imageView);

                            }
                        }).setIndicator(new CircleIndicator(MainActivity.this))
                                //设置banner圆角
                                .setBannerRound(50).start();
                    }
                });

            }
        });
    }
    public void initWebView(){
        ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        webView.loadUrl("https://www.runoob.com/");
        webView.setWebChromeClient(new WebChromeClient(){

            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Snackbar.make(webView,"标题为"+title,Snackbar.LENGTH_SHORT).show();
            }

            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressDialog.setTitle("网页加载进度");
                progressDialog.setCancelable(false);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.show();
                progressDialog.setProgress(newProgress);
                if (newProgress==100){
                    progressDialog.cancel();
                }
            }
        });
    }
    public void initView(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkhttpMethod.getCall("https://api.muxiaoguo.cn/api/yiyan").enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("TAG", "onFailure: "+e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        OneSayBean bean = new Gson().fromJson(response.body().string(),OneSayBean.class);
                        OneSayBean.DataBean dateBean = bean.getData();
                        Snackbar.make(v,dateBean.getConstant()+"\n"+dateBean.getSource(),Snackbar.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}